package net.sipvip.client;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.StackPanel;

public class RightPublicity {

	public static void ipjson(Grid flowpanel, StackPanel stpanel) {

		final Grid nflpanel = flowpanel;
		final StackPanel nstpanel = stpanel;

		JSONRequest.get("http://www.horoskoopit.eu/jsongeneralinfo?callback=",

		new JSONRequestHandler() {

			public void onRequestComplete(JavaScriptObject json) {
				JSONObject j = new JSONObject(json);
				JSONString ip = (JSONString) j.get("ip");
				String ipstr = ip.stringValue();
				geoinfojson(nflpanel, ipstr, nstpanel);

			}

		});

	}

	public static void geoinfojson(Grid flowpanel, String ipstr,
			StackPanel stpanel) {
		
		final Frame frame = new Frame("adsense.html");
		final Grid nflpanel = flowpanel;
		final StackPanel nstpanel = stpanel;

		JSONRequest.get("http://ipinfodb.com/ip_query.php?ip=" + ipstr
				+ "&output=json&callback=",

		new JSONRequestHandler() {

			public void onRequestComplete(JavaScriptObject json) {
				JSONObject j = new JSONObject(json);
				JSONString city = (JSONString) j.get("City");
				JSONString countrycode = j.get("CountryCode").isString();

				String ccity;
				String ccountrycode;

				if (countrycode == null) {
					ccity = "Helsinki";
					ccountrycode = "FI";

				} else {
					ccity = city.stringValue();
					ccountrycode = countrycode.stringValue();
				}

				if (ccountrycode == "FI") {

					nflpanel
							.setWidget(
									1,
									0,
									new RightImage(
											"070095436",
											"Ingrid",
											"http://smsmeeting.eu/pictures/Seksik%C3%A4s%20Ingrid%2038v%20070095436/20317_3.jpgXL.JPG",
											ccity));
					nflpanel.setWidget(2, 0, new RightImage("070095431",
							"Tarja", "/images/Tarja.jpg", ccity));

				} else {
					
					frame.setWidth("200px");
					frame.setHeight("640px");
					nflpanel.setWidget(1, 0, frame);
					DOM.setElementProperty(frame.getElement(), "frameBorder","0");
				}
				nflpanel.setVisible(true);
				

				String cookies = Cookies.getCookie("user");

				if (cookies == null) {

					Date now = new Date();
					long nowLong = now.getTime();
					nowLong = nowLong + (1000 * 60 * 60 * 24 * 364);
					now.setTime(nowLong);

					String cookiesnew = String.valueOf(now.getTime());

					Cookies.setCookie("user", String.valueOf(now.getTime()),
							now);

					userjson(cookiesnew, ccity, ccountrycode, nstpanel);
				} else {

					userjson(cookies, ccity, ccountrycode, nstpanel);
				}

			}

		});

	}

	public static void userjson(String cookies, String city, String country,
			StackPanel stpanel) {

		String urlstr;
		final StackPanel nstpanel = stpanel;

		if (!GWT.isScript()) {
			GWT.log("Hosted mod", null);

			urlstr = "http://localhost:8080/jsongeneralinfo?cookies=" + cookies
					+ "&city=" + city + "&country=" + country + "&callback=";

		} else {
			// TODO deployment
			urlstr = "http://www.seksitreffit.eu/jsongeneralinfo?cookies="
					+ cookies + "&city=" + city + "&country=" + country
					+ "&callback=";

			// urlstr =
			// "http://2.latest.wwwseksitreffiteu.appspot.com/jsongeneralinfo?cookies="
			// + cookies
			// + "&city="
			// + city
			// + "&country="
			// + country
			// + "&callback=";
		}

		JSONRequest.get(urlstr,

		new JSONRequestHandler() {

			public void onRequestComplete(JavaScriptObject json) {

				final FlexTable flexTable = new FlexTable();
				flexTable.setWidth("720px");

				JSONArray jvideo = new JSONArray(json);

				for (int i = 0; i < jvideo.size(); ++i) {
					JSONString videojson = jvideo.get(i).isObject()
							.get("video").isString();
					JSONString imagejson = jvideo.get(i).isObject()
							.get("image").isString();
					JSONString descriptionjson = jvideo.get(i).isObject().get(
							"description").isString();
					JWplayer jwplayer = new JWplayer(videojson.stringValue(),
							imagejson.stringValue(), descriptionjson
									.stringValue(), false);
					DecoratorPanel decoratorpanel = new DecoratorPanel();
					jwplayer.setUrl(imagejson.stringValue());
					Grid smallgrid = new Grid(2, 1);

					smallgrid.setWidget(0, 0, jwplayer);

					String descriptionstr = "";
					if (descriptionjson.stringValue().length() > 15) {
						descriptionstr = descriptionjson.stringValue()
								.substring(0, 15)
								+ "...";
					} else {
						descriptionstr = descriptionjson.stringValue();
					}

					smallgrid.setText(1, 0, descriptionstr);

					int a = (int) i / 5;
					int b = i;

					if (i > 4) {
						b = i - 5;
					}
					if (i > 9) {
						b = i - 10;
					}
					if (i > 14) {
						b = i - 15;
					}
					if (i > 19) {
						b = i - 20;
					}

					decoratorpanel.add(smallgrid);
					flexTable.setWidget(a, b, decoratorpanel);

				}

				if (jvideo.size() > 0) {
					nstpanel.add(flexTable, "Omat videot");
				}

			}

		});

	}
}
